import { Module } from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
import { TypeOrmModule } from '@nestjs/typeorm';
import { resolve } from 'path';
interface MysqlConfig {
  host: string;
  port: number;
  username: string;
  password: string;
  database: string;
}
@Module({
  imports: [
    TypeOrmModule.forRootAsync({
      useFactory(configService: ConfigService) {
        const mysql: MysqlConfig = configService.get('mysql') as MysqlConfig;
        const host =
          process.env.NODE_ENV === 'production'
            ? process.env.DB_HOST || 'mysql'
            : mysql.host;
        console.log('host === ', host);
        return {
          type: 'mysql',
          host,
          port: mysql.port,
          username: mysql.username,
          password: mysql.password,
          database: mysql.database,
          entities: [resolve(__dirname, '../../') + '/**/*.entity{.ts,.js}'], //实体文件
          synchronize: true,
        };
      },
      inject: [ConfigService],
    }),
  ],
  controllers: [],
  providers: [],
})
export class DatabaseModule {}
